package com.loongtech.bi.manager.realTime;

import com.loongtech.bi.entity.realTime.EntityDauTotalByHour;
import com.loongtech.bi.manager.realTime.OverviewManager.OverviewDetailData;
import com.loongtech.core.jpa.manager.ManagerQueryCacheBase;
import com.loongtech.core.util.Time;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 实时数据-小时累计活跃用户manager
 * 
 * @author xckuang
 * @date 2017-10-20
 */
@Service
public class DauTotalByHourManager extends ManagerQueryCacheBase<EntityDauTotalByHour> {
	private static final long serialVersionUID = 1L;
	
	public List<EntityDauTotalByHour> getChannelTotalHourActiveUserByDate(Date date, String queryPlatform, String queryChannel, String queryServer) {
		return findByQuery("from EntityDauTotalByHour where day>=:dayStart and day<:dayEnd and platformid=:platformid and channelid !=:channelid and serverid=:serverid order by hour desc ", "dayStart", new Date(Time.getBeginningOfDay(date.getTime())), "dayEnd", new Date(Time.getBeginningOfDay(date.getTime() + Time.DAY)), "platformid", queryPlatform, "channelid", queryChannel, "serverid", queryServer);
	}
	
	public List<EntityDauTotalByHour> getTotalHourActiveUserByDate(Date date, String queryPlatform, String queryChannel, String queryServer) {
		return findByQuery("from EntityDauTotalByHour where day>=:dayStart and day<:dayEnd and platformid=:platformid and channelid=:channelid and serverid=:serverid order by hour ", "dayStart", new Date(Time.getBeginningOfDay(date.getTime())), "dayEnd", new Date(Time.getBeginningOfDay(date.getTime() + Time.DAY)), "platformid", queryPlatform, "channelid", queryChannel, "serverid", queryServer);
	}
	
	// 根据平台、渠道、区服更新累计小时活跃用户数据
	public void updateTotalHourActiveUserByConditions(String updateType, Map<Integer, OverviewDetailData> map, Date lookDate, Date compareDate, Date lastWeekDate, String queryPlatform, String queryChannel, String queryServer) {
		List<EntityDauTotalByHour> ooLookDate = getTotalHourActiveUserByDate(lookDate, queryPlatform, queryChannel, queryServer);
		List<EntityDauTotalByHour> ooCompareDate = getTotalHourActiveUserByDate(compareDate, queryPlatform, queryChannel, queryServer);
        List<EntityDauTotalByHour> ooLastWeekDate = getTotalHourActiveUserByDate(lastWeekDate, queryPlatform, queryChannel, queryServer);
        if ("1".equals(updateType)) {

        } else if ("2".equals(updateType)) {
            updateData1(map, ooLookDate, ooCompareDate, ooLastWeekDate);
        }
	}
	
    // 更新结果-累计
    private void updateData1(Map<Integer, OverviewDetailData> map, List<EntityDauTotalByHour> ooLookDate, List<EntityDauTotalByHour> ooCompareDate, List<EntityDauTotalByHour> ooLastWeekDate) {
    	for (EntityDauTotalByHour o : ooLookDate) {
			OverviewDetailData odd = map.get(o.getHour());
			if (odd != null) {
				odd.setTotalActiveUserLookValue(o.getDau() + odd.getTotalActiveUserLookValue());
			}
		}
		
		for (EntityDauTotalByHour o : ooCompareDate) {
			OverviewDetailData odd = map.get(o.getHour());
			if (odd != null) {
				odd.setTotalActiveUserCompareValue(o.getDau() + odd.getTotalActiveUserCompareValue());
			}
		}

        for (EntityDauTotalByHour o : ooLastWeekDate) {
            OverviewDetailData odd = map.get(o.getHour());
            if (odd != null) {
                odd.setTotalActiveUserLastWeekValue(o.getDau() + odd.getTotalActiveUserLastWeekValue());
            }
        }
    }
}
